import socket

COD = 'utf-8'
# HOST = 'localhost' # 主机ip

HOST = '119.28.41.16'
PORT = 9090 # 软件端口号

class download():
    tcp_server_socket = None
    recv_data = None
    def __init__(self,host,port):
        try:
            self.tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.tcp_server_socket.connect((HOST, PORT))
        except Exception:
            print("连接失败")
            while self.reconnect():
                print("重连失败")

            self.stayConnected()
            # # 创建TCP套接字
            # tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            # # 链接服务器
            # tcp_socket.connect((HOST, PORT))
            # # 获取下载的文件的名字
            # download_file_name = input("请输入要下载的文件名字：")
            # # 将文件名字发送到服务器
            # tcp_socket.send(download_file_name.encode("utf-8"))
            # # 接收文件中的数据
            # recv_data = tcp_socket.recv(1024)
            # if recv_data:
            #     # 保存接收到的数据到一个新的文件中
            #     with open("[新]" + download_file_name, "wb") as f:
            #         f.write(recv_data)
            # # 关闭套接字
            # tcp_socket.close()

    def stayConnected(self):
        try:
            self.recv_data = self.tcp_server_socket.recv(1024)
            print('服务器:',self.recv_data)
        except Exception:
            print("断开连接")
            self.reconnect()
        if not self.recv_data:
            print("data is null")

            
    def close(self):
        self.tcp_server_socket.close()

    def reconnect(self):
        try:
            print("正在重新连接")
            self.tcp_server_socket.connect((HOST, PORT))
            self.recv_data = self.tcp_server_socket.recv(1024)
            if self.recv_data == 'Success':
                print("连接成功")
                return False
        except Exception:
            return True


    def gitClone(self,url):
        try:
            self.tcp_server_socket.send(url.encode("utf-8"))
        except Exception:
            return
        

if __name__ == '__main__':
    go = download(HOST,PORT)
    while True:
        go.stayConnected()
        url = input('please gitClone URL:')
        go.gitClone(url)

    #go.gitClone('https://github.com/IU2099/test.git')
    #go.gitClone('https://github.com/IU2099/Personal.git')
    #go.gitClone('https://github.com/DuGuQiuBai/Java.gitls')